<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="仪表名称" prop="ybName">
				<el-input v-model="queryParams.ybName" placeholder="请输入仪表名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>

			<el-form-item label="小区名称" prop="communityId">
				<el-select v-model="queryParams.communityId" placeholder="请选择">
					<el-option v-for="item in communitys" :key="item.communityId" :label="item.communityName"
						:value="item.communityId" @keyup.enter.native="handleQuery">
					</el-option>
				</el-select>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['yibiaoManagement:yibiao:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['yibiaoManagement:yibiao:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['yibiaoManagement:yibiao:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['yibiaoManagement:yibiao:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="yibiaoList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="编号" align="center" prop="ybId" />
			<el-table-column label="仪表名称" align="center" prop="ybName" />
			<!-- <el-table-column label="仪表状态" align="center" prop="ybStatus" /> -->

			<el-table-column label="仪表状态" align="center" prop="ybStatus">
				<template v-slot="scope">
					<span>{{ scope.row.ybStatus === 0 ? "启用" : "禁用" }}</span>
				</template>
			</el-table-column>

			<el-table-column label="仪表类型" align="center" prop="wyYbType.ybTpName" />
			<el-table-column label="抄表时间" align="center" prop="ybUpdateTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.ybUpdateTime, "{y}-{m}-{d}") }}</span>
				</template>
			</el-table-column>
			
			
			
			<el-table-column label="备注" align="center" prop="ybReamrk" width="180">
				<template slot-scope="scope">
					{{ delTag(scope.row.ybReamrk) }}
				</template>
			</el-table-column>
			
			<!-- <el-table-column label="备注" align="center" prop="ybReamrk"  /> -->
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">

					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateChaobiao(scope.row)"
						v-hasPermi="['yibiaoManagement:yibiao:edit']" v-if="scope.row.ybStatus === 1 ? false : true " >抄表</el-button>
						
						

					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateStatus(scope.row)"
						v-hasPermi="['yibiaoManagement:yibiao:edit']">{{ scope.row.ybStatus === 0 ? '禁用' : '启用' }}</el-button>

					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['yibiaoManagement:yibiao:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['yibiaoManagement:yibiao:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改仪表管理对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="仪表名称" prop="ybName">
					<el-input v-model="form.ybName" placeholder="请输入仪表名称" />
				</el-form-item>

				<el-form-item label="小区" prop="balconyunithomeId">
					<el-select v-model="queryWbu.communityId" placeholder="请选择小区" @keyup.enter.native="handleQuery">
						<el-option v-for="item in communityList" :key="item.communityId" :label="item.communityName"
							:value="item.communityId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="楼座" prop="balconyunithomeId">
					<el-select v-model="queryWbu.balconyId" placeholder="请选择楼座" @keyup.enter.native="handleQuery">
						<el-option v-for="item in balconyList" :key="item.balconyId" :label="item.balconyName"
							:value="item.balconyId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="单元" prop="balconyunithomeId">
					<el-select v-model="queryWbu.unitId" placeholder="请选择单元" @keyup.enter.native="handleQuery">
						<el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName"
							:value="item.unitId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="房号" prop="balconyunithomeId">
					<el-select v-model="queryWbu.homeId" placeholder="请选择房号" @keyup.enter.native="handleQuery">
						<el-option v-for="item in homeList" :key="item.homeId" :label="item.homeNumber"
							:value="item.homeId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="仪表类型" prop="ybTypeId">
					<el-select v-model="form.ybTypeId" placeholder="请选择">
						<el-option v-for="item in types" :key="item.ybTpId" :label="item.ybTpName" :value="item.ybTpId"
							@keyup.enter.native="handleQuery">
						</el-option>
					</el-select>
				</el-form-item>


				<el-form-item label="备注" >
					<editor v-model="form.ybReamrk" :min-height="192" ></editor>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		chaobiao,
		listTypes,
		setStatus,
		listCommunity,
		listYibiao,
		getYibiao,
		delYibiao,
		addYibiao,
		updateYibiao,
	} from "@/api/yibiaoManagement/yibiao";

	import {
		getCommunity,
		getBalconyUnitHome
	} from "@/api/charge/allocation";

	export default {
		name: "Yibiao",
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 仪表管理表格数据
				yibiaoList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					ybName: null,
					communityId: null,
				},
				// 表单参数
				form: {
					balconyunithomeId: null
				},
				// 表单校验
				rules: {
					balconyunithomeId: [{
						required: true,
						message: '请选择位置信息',
						trigger: 'blur'
					}],
				},
				//所有小区信息
				communitys: [],
				//所有类型
				types: [],

				// 小区列表数据
				communityList: [],
				// 楼座列表数据
				balconyList: [],
				// 单元列表数据
				unitList: [],
				// 房间列表数据
				homeList: [],
				queryWbu: {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				},


			};
		},
		
		created() {
			this.getList();
			this.getCommunityList2();
			this.getTypes();
			this.getCommunityList()
		},
		methods: {
			//去除富文本的标签
			delTag(str){
				return str.replace(/<[^>]+>/g,"");
			},
			/** 查询所有小区信息 */
			getCommunityList2() {
				this.loading = true;
				listCommunity().then((response) => {
					this.communitys = response.rows;
					this.loading = false;
				});
			},
			/** 查询仪表管理列表 */
			getList() {
				this.loading = true;
				listYibiao(this.queryParams).then((response) => {
					this.yibiaoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 查询仪表类型列表 */
			getTypes() {
				this.loading = true;
				listTypes().then((response) => {
					this.types = response.rows;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
				this.queryWbu = {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				};
			},
			// 表单重置
			reset() {
				this.form = {
					ybId: null,
					ybName: null,
					ybStatus: null,
					ybTypeId: null,
					houseId: null,
					yezhuId: null,
					ybCreateTime: null,
					ybUpdateTime: null,
					ybReamrk: null,
					communityId: null,
					balconyId: null,
					del: null,
					unitId: null,
					balconyunithomeId: null
				};
				this.resetForm("form");
				this.queryWbu = {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				};
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map((item) => item.ybId);
				this.single = selection.length !== 1;
				this.multiple = !selection.length;
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加仪表管理";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const ybId = row.ybId || this.ids;
				getYibiao(ybId).then((response) => {
					this.form = response.data;
					this.open = true;
					this.title = "修改仪表管理";
				});
			},
			/** 抄表 */
			handleUpdateChaobiao(row) {
				const ybId = row.ybId;
				chaobiao(ybId).then((response) => {
					this.getList()
				});
			},

			/** 修改启用禁用按钮操作 */
			handleUpdateStatus(row) {
				const ybId = row.ybId;
				setStatus(ybId).then((response) => {
					if (response.code == 200) {
						this.getList()
					}
				});
				this.getList();
			},

			/** 提交按钮 */
			submitForm() {

				this.$refs["form"].validate((valid) => {
					if (valid) {
						if (this.form.ybId != null) {
							updateYibiao(this.form).then((response) => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addYibiao(this.form).then((response) => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ybIds = row.ybId || this.ids;
				this.$modal
					.confirm('是否确认删除仪表管理编号为"' + ybIds + '"的数据项？')
					.then(function() {
						return delYibiao(ybIds);
					})
					.then(() => {
						this.getList();
						this.$modal.msgSuccess("删除成功");
					})
					.catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download(
					"yibiaoManagement/yibiao/export", {
						...this.queryParams,
					},
					`yibiao_${new Date().getTime()}.xlsx`
				);
			},

			/* 获取小区列表 */
			getCommunityList() {
				getCommunity().then(res => {
					this.communityList = res.data
				})
			},
		},
		watch: {
			'queryWbu.communityId': {
				handler() {

					this.balconyList = []
					this.unitList = []
					this.homeList = []
					this.queryWbu.balconyId = null
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null

					this.form.balconyunithomeId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.balconyList = res.data[0]
						// if (res.data.length > 3) {
						// 	// this.queryParams.balconyunithomeId = res.data[3]
						// 	this.form.balconyunithomeId = res.data[3]
						// }
						// this.queryParams.communicateId = this.queryWbu.communityId
						// this.form.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.balconyId': {
				handler() {


					this.unitList = []
					this.homeList = []
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null

					this.form.balconyunithomeId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.unitList = res.data[1]
						// if (res.data.length > 3) {
						// 	// this.queryParams.balconyunithomeId = res.data[3]
						// 	this.form.balconyunithomeId = res.data[3]
						// }
						// this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.unitId': {
				handler() {

					this.homeList = []
					this.queryWbu.homeId = null

					this.form.balconyunithomeId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.homeList = res.data[2]
						// if (res.data.length > 3) {
						// 	// this.queryParams.balconyunithomeId = res.data[3]
						// 	this.form.balconyunithomeId = res.data[3]
						// }
						// this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.homeId': {
				handler(newId) {
					console.log(newId)
					if (newId) {
						getBalconyUnitHome(this.queryWbu).then(res => {
							if (res.data.length > 3) {
								// this.queryParams.balconyunithomeId = res.data[3]
								this.form.balconyunithomeId = res.data[3][0]
								console.log(this.form.balconyunithomeId)
								// console.log(this.form.balconyunithomeId+"--------------")
							}
							// this.queryParams.communicateId = this.queryWbu.communityId
						})
					}

				},
				deep: true,
				immediate: true
			}
		}
	};
</script>